import Vue from 'vue'
import Vuex from 'vuex'
import JMessage from "@/assets/js/jmessage-sdk-web.2.6.0.min.js"
import md5 from 'blueimp-md5'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    JIM:null,
    isLogin:false
  },
  mutations: {
    setJIM(state,JIM){
      state.JIM = JIM
    },
    changeLogin(state,login){
      state.isLogin = login
    }
  },
  actions: {
    init({commit,dispatch}) {
      let timestamp = new Date().getTime()
      const JIM = new JMessage()
      commit('setJIM',JIM)
      JIM.init({
              "appkey":"14ee52f19098774cb48d5505",
              "random_str":  "022cd9fd995849b",
              "signature":  md5(`appkey=14ee52f19098774cb48d5505&timestamp=${timestamp}&random_str=022cd9fd995849b&key=e3aea7056e6ef62b219ad578`),
              "timestamp": timestamp,
              "flag": 1
          }).onSuccess((data)=> {
            dispatch("loginJIM")
      }).onFail(function(data) {
          console.log('error:' + JSON.stringify(data))
      });
    },
    loginJIM({state,commit,dispatch}){
      this.JIM.login({username:this.user.username,password:this.user.username}).onSuccess((data)=> {
          commit('changeLogin',true)
          console.log('success:' + JSON.stringify(data));
      }).onFail((data)=>{
          console.log('error:' + JSON.stringify(data));
      });
    },
    loginOutJIM({state}){
      state.JIM.loginOut()
    }
  },
  modules: {
  }
})
